package com.tanhua.api.impl.mongo;

import com.tanhua.api.mongo.SoundApi;
import com.tanhua.domain.mongo.Sound;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

/**
 * @author ly
 * @date 2021/9/4 0004 14:41
 */
@Service
public class SoundApiImpl implements SoundApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 保存语音的方法
     *
     * @param sound 语音对象
     */
    @Override
    public void save(Sound sound) {
        mongoTemplate.save(sound);
    }


    /**
     * 查询除了当前登录用户的所有语音
     * @param id 当前登录用户id
     * @return List<Sound>
     */
    @Override
    public List<Sound> findList(Integer id) {
        Query query = new Query(Criteria.where("userId").ne(id));
        return mongoTemplate.find(query, Sound.class);
    }


    /***
     * 根据id删除sound
     * @param id sound_id
     */
    @Override
    public void deleteSound(ObjectId id) {
        Query query = new Query(Criteria.where("id").is(id));
        mongoTemplate.remove(query, Sound.class);
    }
}
